#include "gui/cardpanel.h"

#include "gui/button.h"
#include "gui/buttonpanel.h"
#include "gui/guimessage.h"
#include "gui/panel.h"

#include "entities/building.h"
#include "entities/entity.h"
#include "entities/unit.h"

#include "resourcemanager.h"

#define TABWIDTH 80
#define TABHEIGHT 25
#define TABPADDING 15

CardPanel::CardPanel():Widget(){
    name = "CardPanel";
    Setx(10);
    Sety(200);
    SetWidth(800);
    SetHeight(200);
    BuildVBO();

    names = {"Buildings", "Units", "Int. Constr.", "Ext. Constr.", "Recruitment"};

    int padding = 5;
    int border = 5;
    int btnx = border;
    int btny = -border;
    int btnh = 32;
    int btnw = 128;

    for(std::string name : names){
        std::unique_ptr<Button> button(new Button(name, -1, std::unique_ptr<GUIMessage>(new GUIMessage("tab", name))));
        button->SetHeight(btnh);
        button->SetWidth(btnw);
        button->Setx(btnx);
        button->Sety(btny);
        btny = btny - padding - btnh;
        AddChild(std::move(button));

        std::unique_ptr<Panel> panel(new Panel());
        panel->SetName(name);
        panel->SetWidth(640);
        panel->SetHeight(190);
        panel->Setx(148);
        panel->Sety(-5);
        AddChild(std::move(panel));
    }

}

Widget* CardPanel::GetPanel(std::string name){
    for(Widget* child : GetChildren()){
        if(child->GetName()==name and child->GetType()=="Panel"){
            return child;
        }
    }
    std::cout<<"Child name: "<<name<<" not found"<<std::endl;
    return nullptr;
}

void CardPanel::Populate(Entity* entity){
    ClearPreviousEntries();

    FillPanel("Buildings", entity->GetBuildingIDs());
    FillPanel("Buildings", entity->GetConstructionQueueIDs());
    FillPanel("Units", entity->GetUnitIDs());
    FillPanel("Int. Constr.", entity->GetConstructionOptionNames() , "build");
    FillPanel("Ext. Constr.", entity->GetImprovementOptionNames(), "improve");
    FillPanel("Recruitment", entity->GetRecruitmentOptionNames());

    Switch("Buildings");
}

void CardPanel::ClearPreviousEntries(){
    for(std::string name : names){
        GetPanel(name)->DestroyChildren();
    }
}

void CardPanel::FillPanel(std::string panel_name, std::set<int> ids, std::string message_type){
    Widget* panel = GetPanel(panel_name);
    for(int id : ids){
        Entity* entity = gamestate->GetEntity(id);
        AddButton(panel, entity, std::unique_ptr<GUIMessage>(new GUIMessage(message_type, entity->GetName(), entity->GetID())));
    }
}

void CardPanel::FillPanel(std::string panel_name, std::set<std::string> names, std::string message_type){
    Widget* panel = GetPanel(panel_name);
    for(std::string name : names){
        Entity* entity = resourceManager->GetEntity(name);
        AddButton(panel, entity, std::unique_ptr<GUIMessage>(new GUIMessage(message_type, entity->GetName(), entity->GetID())));
    }
}

void CardPanel::AddButton(Widget* panel, Entity* entity, std::unique_ptr<GUIMessage> message){
    if(!panel){
        return;
    }
    std::unique_ptr<Button> button(new Button(entity->GetName(), entity->GetBuildPic()));
    button->SetGUIMessage(std::move(message));
    panel->AddChild(std::move(button));
}

GUIMessage* CardPanel::ProcessMessage(GUIMessage* message){
    if(message->type == "tab"){
        Switch(message->s);
    }
    return message;
}

void CardPanel::Switch(std::string name){
    std::cout<<"Switching to "<<name<<std::endl;
//    building always comes up
    for(Widget* child : GetChildren()){
        if(child->GetType() == "Panel"){
            if(child->GetName() == name){
                child->Show();
            }
            else{
                child->Hide();
            }
        }
    }
}
